using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using AppPriceLive.Entities.Models;
using AppPriceLive.Entities.Utils;
using AppPriceLive.Common;

namespace AppPriceLive.DAL.Base
{
	public class UsersDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public UsersDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Users table.
		/// </summary>
		public virtual void Insert(UsersInfo usersInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", usersInfo.UserName),
				new SqlParameter("@Password", usersInfo.Password),
				new SqlParameter("@Email", usersInfo.Email),
				new SqlParameter("@PhoneNo", usersInfo.PhoneNo),
				new SqlParameter("@DeviceId", usersInfo.DeviceId),
				new SqlParameter("@Status", usersInfo.Status),
				new SqlParameter("@RegisterDate", usersInfo.RegisterDate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Users_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the Users table.
		/// </summary>
		public virtual void Update(UsersInfo usersInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", usersInfo.UserName),
				new SqlParameter("@Password", usersInfo.Password),
				new SqlParameter("@Email", usersInfo.Email),
				new SqlParameter("@PhoneNo", usersInfo.PhoneNo),
				new SqlParameter("@DeviceId", usersInfo.DeviceId),
				new SqlParameter("@Status", usersInfo.Status),
				new SqlParameter("@RegisterDate", usersInfo.RegisterDate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Users_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the Users table by its primary key.
		/// </summary>
		public virtual void Delete(string userName)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", userName)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Users_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Users table.
		/// </summary>
		public virtual UsersInfo Select(string userName)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", userName)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Users_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeUsersInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}


        public virtual UsersInfo UserAuth(string userName, string password)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UserName", userName),
                new SqlParameter("@Password", password)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Users_Auth", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeUsersInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

		/// <summary>
		/// Selects all records from the Users table.
		/// </summary>
		public List<UsersInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Users_SelectAll"))
			{
				List<UsersInfo> usersInfoList = new List<UsersInfo>();
				while (dataReader.Read())
				{
					usersInfoList.Add(MakeUsersInfo(dataReader));
				}

				return usersInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the Users class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual UsersInfo MakeUsersInfo(SqlDataReader dataReader)
		{
			UsersInfo usersInfo = new UsersInfo();
			usersInfo.UserName = SqlClientUtility.GetString(dataReader, DbConstants.USERS.USERNAME);
			usersInfo.Password = SqlClientUtility.GetString(dataReader, DbConstants.USERS.PASSWORD);
			usersInfo.Email = SqlClientUtility.GetString(dataReader, DbConstants.USERS.EMAIL);
			usersInfo.PhoneNo = SqlClientUtility.GetString(dataReader, DbConstants.USERS.PHONENO);
			usersInfo.DeviceId = SqlClientUtility.GetString(dataReader, DbConstants.USERS.DEVICEID);
			usersInfo.Status = SqlClientUtility.GetInt32(dataReader, DbConstants.USERS.STATUS);
			usersInfo.RegisterDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.USERS.REGISTERDATE);

			return usersInfo;
		}

		#endregion
	}
}
